<?php
	namespace Ensao\AppBundle\Controller;
	use Symfony\Bundle\FrameworkBundle\Controller\Controller;
	use Symfony\Component\HttpFoundation\Response;
	use Symfony\Component\HttpFoundation\Request;
	use Symfony\Component\Security\Core\SecurityContext;
	use Symfony\Component\Form\FormBuilder;
	use Ensao\AppBundle\Entity\Client;
	use Ensao\AppBundle\Form\ClientType;
	use Ensao\AppBundle\Form\ClientSingulierForm;

	
	
class ClientController extends Controller
{
	
    //Premiere ouverture de l'app ==> redirection page d'authentification
    public function indexAction()
    {
        $user = $this->container->get('security.context')->getToken()->getUser();
		if($user=='anon.'){
			
			$response = $this->redirect($this->generateUrl('app_login'));
		}
		else {
		
			$response = $this->redirect($this->generateUrl('app_accueil'));
		}
		return $response;
    }
	//Si  l'athentification est faite il est rediriger vers la page d'accueil sinon reauthentification
	public function accueilAction(){
		$user = $this->container->get('security.context')->getToken()->getUser();
		if($user=='anon.'){
			
			$response = $this->redirect($this->generateUrl('app_login'));
		}
		else {
		
		$response = $this->render('EnsaoAppBundle:Client:index.html.twig');
		}
		return $response;
	}
	
	public function clientAction(){
		$user = $this->container->get('security.context')->getToken()->getUser();
		if($user=='anon.'){
			$response = $this->redirect($this->generateUrl('app_login'));
		}
		else {
		$response = $this->render('EnsaoAppBundle:Client:index.html.twig');
		}
		return $response;
	}
	
	public function ajouterAction(Request $request){
			$client1 = new Client;
			$client = $this->createForm(new ClientType, $client1);

			$request = $this->get('request');
			
			if( $request->getMethod() == 'POST' )
			{
				var_dump($request);
				$client->bindRequest($request);
				if( $client->isValid() )
					{
						$em = $this->getDoctrine()->getEntityManager();
						$em->persist($client);
						$em->flush();

						return $this->redirect( $this->generateUrl('app_accueil') );
					}
			}

			return $this->render('EnsaoAppBundle:Client:ajouter.html.twig', array(
			'client' => $client->createView(),
			));

         $response =  $this->render('EnsaoAppBundle:Client:ajouter.html.twig');
		 return $response;
	}	
	
	public function modifierClientSingulierAction($id){
		$em = $this->container->get('doctrine')->getEntityManager();
		$client = $em->getRepository('EnsaoAppBundle:Client')->find($id);
		$form = $this->container->get('form.factory')->create(new ClientSingulierForm(), $client);
		$request =$this->container->get('request');
		if ($request->getMethod() =='POST')
		{
			$form->bindRequest($request);
			if ($form->isValid())
			{
				$em->persist($client);
				$em->flush();
			}
		}		
		return $this->container->get('templating')->renderResponse(
			'EnsaoAppBundle:Client:modifierClientSingulier.html.twig',
			array('form' => $form->createView(),
			'client' => $client)
		);
	}
	
	//redirection vers page 404
	public function errorAction(){
		return $this->render('EnsaoAppBundle:Client:404.html.twig');
	}
}